package main_run;

import java.io.*;

import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class main extends MIDlet implements CommandListener,Runnable {
	private Display mDisplay;
	private Command mExitCommand,mFindCommand,mCancelCommand;
	private TextBox mSubmitBox;
	private Form mProgressForm;
	private StringItem mProgressString;

	public main() {
		mExitCommand = new Command("Exit", Command.EXIT, 0);
		mFindCommand = new Command("Find",Command.SCREEN,0);
		mCancelCommand = new Command("Find",Command.CANCEL,0);
		
		mSubmitBox = new TextBox("TestBox", "", 32, 0);
		mSubmitBox.addCommand(mExitCommand);
		mSubmitBox.addCommand(mFindCommand);
		mSubmitBox.setCommandListener(this);
		
		mProgressForm = new Form("Lookup Progress");
		mProgressString = new StringItem(null, null);
		mProgressForm.append(mProgressString);
				
	}

	public void run() {
		String word = mSubmitBox.getString();
		Image _icon = null;
		String definition;
		try {
			definition = lookUp(word);
			_icon  = Image.createImage("cancel.png"); 
		} catch (IOException e) {
				Alert report = new Alert("Sorry", "Something went wrong and that defination could not be retrieved.",_icon,null);
				report.setTimeout(Alert.FOREVER);
				mDisplay.setCurrent(report,mSubmitBox);
				return;

		}
		
		Alert results = new Alert("Definition",definition,null,null);
		results.setTimeout(Alert.FOREVER);
		mDisplay.setCurrent(results,mSubmitBox);
	}

	public void commandAction(Command c, Displayable d) {
		if(c== mExitCommand){
			try {
				destroyApp(false);
				notifyDestroyed();
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
		}
		else if(c == mFindCommand){
			mDisplay.setCurrent(mProgressForm);
			Thread t = new Thread(this);
			t.start();
		}
		
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		mDisplay = Display.getDisplay(this);
		mDisplay.setCurrent(mSubmitBox);
		
	}
	private String lookUp(String word) throws IOException{
		HttpConnection hc = null;
		InputStream in = null;
		String definition = null;
		try {
			String baseURL =  "http://www.dict.org/bin/Dict";
			String url =  baseURL +word;
			mProgressString.setText("connecting......");
			hc = (HttpConnection)Connector.open(baseURL);
			in = hc.openInputStream();
			
			mProgressString.setText("reading....");
			int contentLenght = (int)hc.getLength();
			if(contentLenght == -1) contentLenght =255;
			byte[] raw = new byte[contentLenght];
			int lenght = in.read(raw);
			
			in.close();
			hc.close();
			
			definition = new String(raw,0,lenght);
			
		}finally{
			try {
				if(in != null) in.close();
				if(hc != null) hc.close();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return definition;
		
	}
	

}
